/// @tags: ST
#include <cstdio>
#include <iostream>
#include <queue>
#define rint register int

using std::cin;
using std::cout;

namespace BlueQuantum {

constexpr int MAXN = 2e5 + 3, ST = 18, INF = 0x3f3f3f3f;

int n;
int p[MAXN];
int ecnt;
int heads[MAXN];

struct Edge {
  int v, nxt;
} e[MAXN];

inline void add(int u, int v) { e[++ecnt].v = v, e[ecnt].nxt = heads[u], heads[u] = ecnt; }

struct STable {
  int st[ST][MAXN];
  static int lg[MAXN];

  static inline int cmp(int a, int b) { return p[a] < p[b] ? a : b; }

  static inline void init() {
    for (rint i = 2; i <= n; ++i) lg[i] = lg[i / 2] + 1;
  }

  inline void build() {
    for (rint i = 1; i < ST; ++i) {
      for (rint j = 1; j + (1 << i) - 1 <= n; ++j) {
        st[i][j] = cmp(st[i - 1][j], st[i - 1][j + (1 << (i - 1))]);
      }
    }
  }

  inline int query(int l, int r) {
    rint k = lg[r - l + 1];
    return cmp(st[k][l], st[k][r - (1 << k) + 1]);
  }
} st[2];

typedef std::pair<int, int> PII;

PII ans[MAXN];

void solve(int l, int r, int cur, int fa, int target, int lbrace) {
  if (l > r) return;
  int pos = st[cur].query(l, r);
  if (lbrace)
    ans[lbrace] = ans[p[pos]] = std::make_pair(lbrace, p[pos]);
  else
    add(fa, p[pos]);
  if (target) {
    solve(l, pos - 1, ((pos - 1) & 1) ^ 1, lbrace, 0, 0);
    solve(pos + 1, r, (r & 1) ^ 1, lbrace, 0, 0);
  } else {
    solve(l, pos - 1, ((pos - 1) & 1) ^ 1, p[pos], 0, 0);
    solve(pos + 1, r, r & 1, p[pos], 1, p[pos]);
  }
}

int STable::lg[MAXN];
std::priority_queue<PII, std::vector<PII>, std::greater<PII> > pq;

inline int main() {
  cin >> n;
  p[0] = INF;
  for (rint i = 1; i <= n; ++i) {
    cin >> p[i];
    st[i & 1].st[0][i] = i;
  }
  STable::init();
  st[0].build(), st[1].build();
  solve(1, n, (n & 1) ^ 1, n + 1, 0, 0);
  pq.emplace(e[heads[n + 1]].v, 0);
  while (!pq.empty()) {
    int u = pq.top().first;
    if (u) cout << ans[u].first << ' ' << ans[u].second << ' ';
    pq.pop();
    for (int i = heads[u]; i; i = e[i].nxt) {
      pq.emplace(ans[e[i].v]);
    }
  }
  return 0;
}

}  // namespace BlueQuantum

int main() {
#ifndef ONLINE_JUDGE
#ifdef LOCAL
  freopen("/tmp/CodeTmp/testdata.in", "r", stdin);
  freopen("/tmp/CodeTmp/testdata.out", "w", stdout);
#else
  freopen("seq.in", "r", stdin);
  freopen("seq.out", "w", stdout);
#endif
#endif

  std::ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
  return BlueQuantum::main();
}
